<html>
<head>
    <title>Search For</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <script type="text/javascript" src="<?=base_url()?>resource/js/jquery.js"></script>
    
    <link rel="stylesheet" href='<?=base_url()?>resource/css/style.css' type="text/css"/>
    
    <link rel="stylesheet" type="text/css" href="<?=base_url()?>resource/css/jquery.datetimepicker.css" />
    <script src="<?=base_url()?>resource/js/jquery.datetimepicker.js"></script>
    
    <link rel="stylesheet" type="text/css" href="<?=base_url()?>resource/css/jquery.autocompleter.css" />
    <script src="<?=base_url()?>resource/js/jquery.autocompleter.js"></script>
</head>
<body>
    <div class="house-header">
        <h3>Tenement</h3>
    </div>
    <div class="input-div">
        <form action="<?=base_url('profile/tenement/'.$worker['id'])?>" method="post" onkeydown="if(event.keyCode==13)return false;" onsubmit="return checkform()">
        <input type="hidden" name="houseconpanyid" value="<?=$houseconpanyid?>" />
        <table>
            <tr><td colspan="2"><h3>.<?=$title?></h3></td></tr>
            <tr><td class="nametd">Address:</td><td><input type="text" name="address" value="<?=$worker['address']?>"/><span class="err" id="addressmsg">*</span></td></tr>
            <tr><td class="nametd">Rent:</td><td><input type="text" name="rent" value="<?=$worker['rent']?>"/><span class="err" id="rentmsg">*</span></td></tr>
            <tr><td class="nametd">House Agent:</td>
                <td class="relative">
                    <div class="field">
                        <input type="text" name="houseagent" value="<?=$houseagent['name']?>"/>
                    </div>
                    <span id="agentmsg" class="err">*</span>
                </td>
            </tr>
            <tr><td class="nametd">Agent Address:</td><td id="agent_address"><?=$houseagent['address']?></td></tr>
            <tr><td class="nametd">Agent Email:</td><td id="agent_email"><?=$houseagent['email']?></td></tr>
            <tr><td class="nametd">Contact Person:</td><td id="agent_person"><?=$houseagent['person']?></td></tr>
            <tr><td class="nametd">Contact Number:</td><td id="agent_number"><?=$houseagent['number']?></td></tr>
            <tr><td class="nametd">Comment:</td><td id="agent_comment"><?=$houseagent['comment']?></td></tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Save"/>
                </td>
            </tr>
        </table>
        </form>
    </div>
<script>
var data = new Array();

<?php foreach ($houseagents as $key => $value) { ?>
    data[<?=$key?>] = new Array();
    data[<?=$key?>]['label'] = '<?=$value['name']?>';
    data[<?=$key?>]['id'] = '<?=$value['id']?>';
    data[<?=$key?>]['address'] = '<?=$value['address']?>';
    data[<?=$key?>]['email'] = '<?=$value['email']?>';
    data[<?=$key?>]['person'] = '<?=$value['person']?>';
    data[<?=$key?>]['number'] = '<?=$value['number']?>';
    data[<?=$key?>]['comment'] = '<?=$value['comment']?>';
<?php } ?>

$(function () {
    $("input[name='houseagent']").autocompleter({ 
        source: data,
        highlightMatches: true,
        template: '{{ label }}',
        hint: true,
        empty: false,
        callback: function (value, index, selected) {
            if (selected) {
                $("input[name='houseconpanyid']").val(selected.id);
                $("#agent_address").html(selected.address);
                $("#agent_email").html(selected.email);
                $("#agent_person").html(selected.person);
                $("#agent_number").html(selected.number);
                $("#agent_comment").html(selected.comment);
            }
        }
    });
});

function checkform(){
    var flag = true;
    if($.trim($("input[name='address']").val()) == ''){
        $("#addressmsg").html("Address can not be empty!");
        flag = false;
    }else{
        $("#addressmsg").html("*");
    }
    if($.trim($("input[name='rent']").val()) == ''){
        $("#rentmsg").html("Rent can not be empty!");
        flag = false;
    }else{
        $("#rentmsg").html("*");
    }
    var agentname = $.trim($("input[name='houseagent']").val());
    if(agentname == ''){
        $("#agentmsg").html("Agent Name can not be empty!");
        flag = false;
    }else{
        $("#agentmsg").html("*");
    }
    
    if(!flag){
        return flag;
    }
    
    var companyflag = false;
    for(var i = 0;i < data.length;i++){
        if(data[i]['label'] == agentname){
            $("input[name='houseconpanyid']").val(data[i]['id']);
            companyflag = true;
        }
    }
    if(!companyflag){
        $("input[name='houseconpanyid']").val('');
        $("#agentmsg").html("The house agent don't exist!");
        return false;
    }
    return flag;
}
</script>
</body>
</html>